﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SnippetBox.Core.Common.Extensions;
using SnippetBox.Core.Common.Paging;
using SnippetBox.Core.Data.Models;
using SnippetBox.Core.Data.Service;
using SnippetBox.Web.Models;

namespace SnippetBox.Web.Controllers
{
    public class SearchController : Controller
    {
        public ActionResult Index(string query, int? page)
        {
            if (query.IsNullOrEmpty())
                return View(new SearchModel());

            SnippetService snippetService = new SnippetService();
            var snippets = snippetService.Search(query, page);

            CategoryService categoryService = new CategoryService();
            IPagedList<Category> categories = categoryService.Search(query, 1, 100, splitSearch:true);

            SearchModel model = new SearchModel(query, snippets, categories);

            return View(model);
        }

    }
}
